package cloud.xlh.digital_power_order.service.order.adapter;

import cloud.xlh.digital_power_order.domian.order.OrderSaveDomain;
import cloud.xlh.digital_power_order.domian.order.OrderUpdateDomain;
import cloud.xlh.digital_power_order.infrastructure.order.entity.OrderEntity;
import cloud.xlh.digital_power_order.util.cache.OrderRedisOperateServiceImpl;
import cloud.xlh.infrastructure.utils.CommonUtil;
import cloud.xlh.infrastructure.utils.DateUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : xlh_osms
 * @ Author: XuLeHuang
 * @ Date: 2023/8/12 19:16
 * @ Description:
 */
@Component
public class OrderServiceAdapter {
    private static final String ORDER_NO_KEY = "OrderNo";

    @Resource(name = "orderRedis")
    private OrderRedisOperateServiceImpl orderRedisOperateServiceImpl;

    public OrderEntity convertOrderEntity(OrderEntity orderEntity , OrderSaveDomain orderSaveDomain) {
        if (orderEntity == null) {
            orderEntity = new OrderEntity();
        }
        CommonUtil.copyPropertiesIgnoreCase(orderSaveDomain, orderEntity);
        String orderNo = String.valueOf(orderRedisOperateServiceImpl.getOrderNoFromSortCollection(ORDER_NO_KEY));
        //不在订单号生成的task拼接日期是为了节省订单号资源，同时不用再去清理昨天没有用完的订单号
        orderEntity.setOrderNo(DateUtil.toAppointPatternLocalDateStr(LocalDate.now() , DateUtil.YYYYMMDD_FORMAT).concat(orderNo));
        orderEntity.setCreateTime(DateUtil.toAppointPatternLocalDateTimeStr(LocalDateTime.now()  , DateUtil.HHMMSS_FORMAT));
        orderEntity.setUpdateTime(DateUtil.toAppointPatternLocalDateTimeStr(LocalDateTime.now()  , DateUtil.HHMMSS_FORMAT));
        return orderEntity;
    }

    public OrderSaveDomain convertOrderSaveDomain(OrderSaveDomain orderSaveDomain , OrderEntity orderEntity) {
        if (orderSaveDomain == null) {
            orderSaveDomain = new OrderSaveDomain();
        }
        CommonUtil.copyPropertiesIgnoreCase(orderEntity, orderSaveDomain);
        return orderSaveDomain;
    }

    public OrderEntity convertOrderEntity(OrderUpdateDomain orderUpdateDomain) {
        OrderEntity orderEntity = new OrderEntity();
        CommonUtil.copyPropertiesIgnoreCase(orderUpdateDomain, orderEntity);
        orderEntity.setUpdateTime(DateUtil.toAppointPatternLocalDateTimeStr(LocalDateTime.now()  , DateUtil.HHMMSS_FORMAT));
        return orderEntity;
    }
}
